#include <vector>
#include <iostream>
#include <functional>

using namespace std;

int citys(vector<vector<int> >& m) {
    int n = m.size();
    vector<bool> vis(n, false);
    int res = 0;
    function<void(int)> dfs = [&](int cur) {
        vis[cur] = true;
        for (int i = 0; i < n; ++i)
        {
            if (m[cur][i] == 1 && !vis[i])
                dfs(i);
        }
    };
    for (int i = 0; i < n; ++i)
    {
        if (!vis[i])
        {
            ++res;
            dfs(i);
        }
    }
    return res;
}

class Solution1 {
public:
    int countTestedDevices(vector<int>& batteryPercentages) {
        int res = 0; int cur = 0;
        for (int item : batteryPercentages)
        {
            if (item - cur > 0)
            {
                ++res;
                ++cur;
            }
        }
        return res;
    }
};

struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

int TreeHight(TreeNode* root)
{
    if (root == nullptr) return 0;
    int lefth = TreeHight(root->left);
    int righth = TreeHight(root->right);

    if (lefth == -1 || righth == -1) return -1;
    return abs(lefth - righth) <= 1 ? max(lefth, righth) + 1 : -1;
}

bool IsBalanced_Solution(TreeNode* pRoot) {
    if (pRoot == nullptr) return true;

    return TreeHight(pRoot) != -1;
}

void solve()
{
    int n; cin >> n;
    vector<vector<int>> m(n + 1, vector<int>(n + 1));
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            cin >> m[i][j];

    vector<vector<int>> sums(n + 1, vector<int>(n + 1));
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            sums[i][j] = sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1] + m[i][j];

    int res = m[1][1];
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            for (int x = i; x <= n; ++x)
                for (int y = j; y <= n; ++y)
                    res = max(res, sums[x][y] - sums[x][j - 1] - sums[i - 1][y] + sums[i - 1][j - 1]);

    cout << res;

}

class Solution2 {
public:
    int garbageCollection(vector<string>& garbage, vector<int>& travel) {
        int n = garbage.size();
        for (int i = 1; i < n - 1; ++i)
            travel[i] += travel[i - 1];

        int res = 0;
        int mlast = 0, plast = 0, glast = 0;
        for (int i = 0; i < n; ++i)
        {
            bool mcnt = false, pcnt = false, gcnt = false;
            for (char c : garbage[i])
            {
                if (c == 'M') mcnt = true;
                else if (c == 'P') pcnt = true;
                else gcnt = true;
            }
            res += garbage[i].size();
            if (mcnt) mlast = i;
            if (pcnt) plast = i;
            if (gcnt) glast = i;
        }

        if (mlast - 1 >= 0) res += travel[mlast - 1];
        if (plast - 1 >= 0) res += travel[plast - 1];
        if (glast - 1 >= 0) res += travel[glast - 1];
        return res;
    }
};

int main()
{
    vector<string> v{ "G","P","GP","GG" };
    vector<int> t{ 2,4,3 };
    Solution2().garbageCollection(v, t);
    return 0;
}